package com.example.glgk.controller.supportFile;

import com.example.glgk.dto.supportFile.SupportMaterialFileDto;
import com.example.glgk.service.report.ReportService;
import com.example.glgk.service.supportFile.ISupportFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @author lxk
 * <p>指标上报controller</p>
 */
@Api(tags = "佐证材料上传controller")
@Slf4j
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/SupportFileController")
public class SupportFileController {


    @Autowired
    private ISupportFileService supportFileService;




    @GetMapping("/searchSupportFile")
    @ApiOperation(value = "查询佐证材料")
    public List<SupportMaterialFileDto> searchSupportFile(@RequestParam("dept") String dept,@RequestParam("taskId") String taskId){

        List<SupportMaterialFileDto> list = supportFileService.searchSupportFile(dept,taskId);
        return list;
    }


    @PostMapping("/upload")
    @ApiOperation(value = "上传佐证材料")
    public String upload(@RequestParam("file") MultipartFile[] files, @RequestParam("deptId")String dept,@RequestParam("supportId")Integer supportId,@RequestParam("fileId")Integer fileId,@RequestParam("taskId") String taskId) {
        try {
            return supportFileService.upload(files,dept,supportId,fileId,taskId);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    @GetMapping("/deleteFile")
    @ApiOperation(value = "删除佐证材料")
    public String deleteFile(@RequestParam("fileId")Integer fileId,@RequestParam("path") String path,@RequestParam("fileNames")String[] fileNames)  {
        if(StringUtils.isEmpty(fileId)){
            return "文件Id不能为空";
        }
        return supportFileService.deleteFile(fileId,path,fileNames);
    }


    @GetMapping("/queryAdditionalFile")
    @ApiOperation(value = "查询附加材料")
    public List<Map<String,String>> queryAdditionalFile(@RequestParam("dept") String dept
            , @RequestParam("taskId") String taskId,@RequestParam("indexId")String indexId){

        return supportFileService.queryAdditionalFile(dept, taskId, indexId);
    }

    @PostMapping("/uploadAdditionalFile")
    @ApiOperation(value = "上传附加材料")
    public String uploadAdditionalFile(@RequestParam("file") MultipartFile[] files, @RequestParam("dept")String dept,@RequestParam("indexId")String indexId,@RequestParam("fileId")String fileId,@RequestParam("taskId")String taskId)  {

        try {
            return supportFileService.uploadAdditionalFile(files,dept,indexId,fileId,taskId);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @GetMapping("/deleteAdditionalFile")
    @ApiOperation(value = "删除附加材料")
    public String deleteAdditionalFile(@RequestParam("fileId")String fileId,@RequestParam("path") String path,@RequestParam("fileNames")String[] fileNames)  {
        if(StringUtils.isEmpty(fileId)){
            return "文件Id不能为空";
        }
        return supportFileService.deleteAdditionalFile(fileId,path,fileNames);
    }

}